package org.jeecg.modules.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wms.entity.WmsInventoryHistory;
import org.jeecg.modules.wms.entity.WmsMaterial;
import org.jeecg.modules.wms.service.IWmsInventoryHistoryService;
import org.jeecg.modules.wms.service.IWmsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存历史记录
 */
@Slf4j
@Api(tags = "库存历史记录")
@RestController
@RequestMapping("/wms/inventoryHistory")
public class WmsInventoryHistoryController extends JeecgController<WmsInventoryHistory, IWmsInventoryHistoryService> {

    @Autowired
    private IWmsInventoryHistoryService inventoryHistoryService;

    @Autowired
    private IWmsMaterialService materialService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "库存历史记录-分页列表查询")
    @ApiOperation(value = "库存历史记录-分页列表查询", notes = "库存历史记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WmsInventoryHistory>> queryPageList(WmsInventoryHistory history,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Page<WmsInventoryHistory> page = new Page<>(pageNo, pageSize);
        
        // 构建查询条件Map
        Map<String, Object> query = new HashMap<>();
        
        // 处理基本查询条件
        if (history != null) {
            if (StringUtils.isNotBlank(history.getMaterialCode())) {
                query.put("materialCode", history.getMaterialCode());
            }
            if (history.getMaterialType() != null) {
                query.put("materialType", history.getMaterialType());
            }
            if (StringUtils.isNotBlank(history.getWarehouseId())) {
                query.put("warehouseId", history.getWarehouseId());
            }
            if (StringUtils.isNotBlank(history.getLocationId())) {
                query.put("locationId", history.getLocationId());
            }
            if (history.getBusinessType() != null) {
                query.put("businessType", history.getBusinessType());
            }
            if (StringUtils.isNotBlank(history.getBusinessNo())) {
                query.put("businessNo", history.getBusinessNo());
            }
            if (StringUtils.isNotBlank(history.getBatchNo())) {
                query.put("batchNo", history.getBatchNo());
            }
            if(history.getMaterialSeries() != null){
                query.put("materialSeries", history.getMaterialSeries());
            }
            if(history.getColor() != null){
                query.put("color", history.getColor());
            }
        }
        
        // 处理时间范围查询
        String beginDate = req.getParameter("operateTimeStart");
        if (!oConvertUtils.isEmpty(beginDate)) {
            query.put("operateTimeStart", beginDate);
        }
        String endDate = req.getParameter("operateTimeEnd");
        if (!oConvertUtils.isEmpty(endDate)) {
            query.put("operateTimeEnd", endDate);
        }
        
        IPage<WmsInventoryHistory> pageList = inventoryHistoryService.queryHistoryPageList(page, query);
        return Result.OK(pageList);
    }

    /**
     * 添加
     */
    @AutoLog(value = "库存历史记录-添加")
    @ApiOperation(value = "库存历史记录-添加", notes = "库存历史记录-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WmsInventoryHistory history) {
        inventoryHistoryService.save(history);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "库存历史记录-编辑")
    @ApiOperation(value = "库存历史记录-编辑", notes = "库存历史记录-编辑")
    @PutMapping(value = "/edit")
    public Result<String> edit(@RequestBody WmsInventoryHistory history) {
        inventoryHistoryService.updateById(history);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "库存历史记录-通过id删除")
    @ApiOperation(value = "库存历史记录-通过id删除", notes = "库存历史记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        inventoryHistoryService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "库存历史记录-批量删除")
    @ApiOperation(value = "库存历史记录-批量删除", notes = "库存历史记录-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.inventoryHistoryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "库存历史记录-通过id查询")
    @ApiOperation(value = "库存历史记录-通过id查询", notes = "库存历史记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsInventoryHistory> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsInventoryHistory history = inventoryHistoryService.getById(id);
        return Result.OK(history);
    }
    
    /**
     * 查询库存历史记录列表
     */
    @AutoLog(value = "库存历史记录-查询库存历史记录列表")
    @ApiOperation(value = "库存历史记录-查询库存历史记录列表", notes = "库存历史记录-查询库存历史记录列表")
    @GetMapping(value = "/queryHistoryList")
    public Result<List<WmsInventoryHistory>> queryHistoryList(@RequestParam(name = "inventoryId", required = false) String inventoryId,
                                                           @RequestParam(name = "materialCode", required = false) String materialCode,
                                                           @RequestParam(name = "warehouseId", required = false) String warehouseId,
                                                           @RequestParam(name = "locationId", required = false) String locationId,
                                                           @RequestParam(name = "businessType", required = false) Integer businessType,
                                                           @RequestParam(name = "businessNo", required = false) String businessNo) {
        Map<String, Object> query = new HashMap<>();
        
        if (StringUtils.isNotBlank(inventoryId)) {
            query.put("inventoryId", inventoryId);
        }
        if (StringUtils.isNotBlank(materialCode)) {
            query.put("materialCode", materialCode);
        }
        if (StringUtils.isNotBlank(warehouseId)) {
            query.put("warehouseId", warehouseId);
        }
        if (StringUtils.isNotBlank(locationId)) {
            query.put("locationId", locationId);
        }
        if (businessType != null) {
            query.put("businessType", businessType);
        }
        if (StringUtils.isNotBlank(businessNo)) {
            query.put("businessNo", businessNo);
        }
        
        List<WmsInventoryHistory> list = inventoryHistoryService.queryHistoryList(query);
        return Result.OK(list);
    }
    
    /**
     * 获取库存历史统计分析数据
     */
    @AutoLog(value = "库存历史记录-统计分析")
    @ApiOperation(value = "库存历史记录-统计分析", notes = "库存历史记录-统计分析")
    @GetMapping(value = "/getStatisticsData")
    public Result<Map<String, Object>> getStatisticsData(
            @RequestParam(name = "timeRange", defaultValue = "month") String timeRange,
            @RequestParam(name = "materialType", required = false) String materialType,
            @RequestParam(name = "materialCode", required = false) String materialCode,
            @RequestParam(name = "warehouseId", required = false) String warehouseId,
            @RequestParam(name = "locationId", required = false) String locationId,
            @RequestParam(name = "businessType", required = false) Integer businessType,
            @RequestParam(name = "businessNo", required = false) String businessNo) {
        
        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("timeRange", timeRange); // day, week, month, year
        
        if (StringUtils.isNotBlank(materialType)) {
            params.put("materialType", materialType);
        }
        if (StringUtils.isNotBlank(materialCode)) {
            params.put("materialCode", materialCode);
        }
        if (StringUtils.isNotBlank(warehouseId)) {
            params.put("warehouseId", warehouseId);
        }
        if (StringUtils.isNotBlank(locationId)) {
            params.put("locationId", locationId);
        }
        if (businessType != null) {
            params.put("businessType", businessType);
        }
        if (StringUtils.isNotBlank(businessNo)) {
            params.put("businessNo", businessNo);
        }
        
        // 调用服务层方法获取统计数据
        Map<String, Object> statisticsData = inventoryHistoryService.getStatisticsData(params);
        
        return Result.OK(statisticsData);
    }
} 